﻿using System;
using System.Windows.Controls;
using Phoenix.Frames;
using Phoenix.Framework;
using Phoenix.Lifetime;
using Xunit;

namespace Phoenix.Tests.Navigation
{
    public class NavigationRequestTests
    {
        [Fact]
        public void when_using_weak_reference_uncollected_view_can_be_retreived()
        {
            // arrange
            var view = new UserControl();
            var navigationRequest = new NavigationRequest(
                new ControllerAction(string.Empty, string.Empty, null), null, view,
                new RequestTransientStore(),
                InvokeOptions.UseWeakReference);

            // act / assert
            Assert.Equal(view, navigationRequest.View);
        }

        [Fact]
        public void use_weak_reference_flag_allows_gc_to_collect_view()
        {
            // arrange
            var navigationRequest = CreateNavigationRequest();

            // act
            GC.Collect();

            // assert
            Assert.Null(navigationRequest.View);
        }

        static NavigationRequest CreateNavigationRequest()
        {
            return new NavigationRequest(
                new ControllerAction(string.Empty, string.Empty, null), null, new UserControl(),
                new RequestTransientStore(),
                InvokeOptions.UseWeakReference);
        }

        [Fact]
        public void use_weak_reference_flag_false_does_not_allow_view_to_be_collected()
        {
            // arrange
            var navigationRequest = new NavigationRequest(
                new ControllerAction(string.Empty, string.Empty, null), null, new UserControl(),
                new RequestTransientStore(), InvokeOptions.None);

            // act
            GC.Collect();

            // assert
            Assert.NotNull(navigationRequest.View);
        }
    }
}